package at.guigu.resolver;

import at.guigu.exception.MyException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyExceptionResolver implements HandlerExceptionResolver {
    /**
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e:为抛出的异常对象
     * @return :返回ModelAndView对象，为异常映射的视图
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        // 若是ClassCastException异常则映射到errorShow1.jsp页面
        if (e instanceof ClassCastException) {
            modelAndView.setViewName("errorShow1");
        // 若是自定义MyException异常则映射到errorShow5.jsp页面
        } else if (e instanceof MyException) {
            modelAndView.setViewName("errorShow5");
        // 若是其它异常则映射到error.jsp页面
        } else {
            modelAndView.setViewName("error");
        }
        return  modelAndView;
    }
}
